package com.lrkj.back.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.StartUpOnLoadData;
import com.lrkj.back.entity.TbPackageMuban;
import com.lrkj.back.entity.ZsHuishou;
import com.lrkj.back.entity.ZsPackageImformation;
import com.lrkj.back.service.TbPackageMubanService;
import com.lrkj.back.service.ZsPackageImformationService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.cbons.mamisay.parent.exception.BusinessException;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/youtiaoma")
public class ZsHuishouYoutiaomaController extends BaseController{
	
	@Autowired
	private ZsPackageImformationService zsPackService;
	@Autowired
	private TbPackageMubanService tbPackMubanService;
	
	
	@RequestMapping("/findmuban.do")
	@ResponseBody
	public Map packageingSave( HttpServletRequest request,
                               HttpServletResponse response) throws BusinessException{
		
		Map<String, Object> rtnMap = new HashMap<>();
		Map<String,ZsPackageImformation> map = new HashMap<>();
		try {
			String list = request.getParameter("dataList");
			String wanglan = request.getParameter("wanglan");
			JSONArray jsonArray = JSON.parseArray(list);
			//消除jsonpackageTiaoma 重复的json数据。
			//如果使用相同
//			Map<String,ZsPackageImformation> wlmap = new HashMap<>();
			List<ZsPackageImformation> zsPackageImformations = new ArrayList<>();
			JSONObject jot ;
//			for(int m =0;m<jsonArray.size();m++){
//				jot = jsonArray.getJSONObject(m);
//				String packageBarcode = jot.getString("bao");
//				String wlBarcode = jot.getString("lan");
//				ZsPackageImformation zsPackageImformation  =  zsPackService.selectByPrimaryKey(packageBarcode);
//				wlmap.put(wlBarcode,zsPackageImformation);
//			}
			for(int m =0;m<jsonArray.size();m++){
				String packageBarcode = jsonArray.getString(m);
				ZsPackageImformation zsPackageImformation  =  zsPackService.selectByPrimaryKey(packageBarcode);
				if(zsPackageImformation!=null){
					zsPackageImformations.add(zsPackageImformation);
				}
			}
			rtnMap.put("zsPackageImformation", zsPackageImformations);
			rtnMap.put("wanglan",wanglan);
			rtnMap.put("depMap", StartUpOnLoadData.depMap);
			rtnMap.put("status", "true");
			rtnMap.put("msg", "添加成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "添加失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping("/findbao.do")
	@ResponseBody
	public Map findbao(String baoTiaoma) throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		ZsPackageImformation packageImformation = zsPackService.selectByPrimaryKey(baoTiaoma);
		if (packageImformation!=null) {
			rtnMap.put("status", "true");
			rtnMap.put("msg", "");
		}else {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "");
			rtnMap.put("packaInfo",packageImformation);
		}
		return rtnMap;
	}
}

/**
 * 封装包得条码和网篮得条码
 * @author xvyong
 *
 */
class PackWanglan{
	public PackWanglan(){
		
	}
	public PackWanglan(String packTM, String wanglan) {
		super();
		this.packTM = packTM;
		this.wanglan = wanglan;
	}
	String packTM;
	String wanglan;
	public String getPackTM() {
		return packTM;
	}
	public void setPackTM(String packTM) {
		this.packTM = packTM;
	}
	public String getWanglan() {
		return wanglan;
	}
	public void setWanglan(String wanglan) {
		this.wanglan = wanglan;
	}
}
